﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.SceneSwitchOperation
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;

#nullable disable
namespace Netick.Unity;

internal class SceneSwitchOperation : SceneOperation
{
  public NetworkSceneOperation NetworkSceneOperation;
  public int SceneIndex;
  public HashSet<Scene> PrevScenes;
  public int SceneCounter;
  private bool LoadedScene;
  private bool UnloadedPrevScene;
  public Scene NewScene;
  public ISceneOperation LoadOp;
  private List<ISceneOperation> UnloadOps;

  public NetworkSandbox Sandbox { get; set; }

  public bool IsFinished { get; set; }

  void SceneOperation.Start()
  {
    StaticUnitySceneManager.ValidateSceneId(this.Sandbox, this.SceneIndex);
    if (StaticUnitySceneManager.IsCustomScene(this.SceneIndex))
    {
      int num = this.SceneIndex - SceneManager.sceneCountInBuildSettings;
      string sceneName;
      this.LoadOp = this.Sandbox.NetworkSceneHandler.InvokeLoadCustomSceneAsync(num, this.Sandbox.GetDefaultLoadSceneParameters(), out sceneName);
      this.NetworkSceneOperation = new NetworkSceneOperation(this.LoadOp, new Scene(), true, num, sceneName, false);
    }
    else
    {
      this.LoadOp = this.Sandbox.NetworkSceneHandler.InvokeLoadBuildSceneAsync(this.SceneIndex, this.Sandbox.GetDefaultLoadSceneParameters());
      this.NetworkSceneOperation = new NetworkSceneOperation(this.LoadOp, SceneManager.GetSceneAt(SceneManager.sceneCount - 1), true, this.SceneIndex, Network.NameFromIndex(this.SceneIndex), false);
    }
    this.Sandbox.Events.InvokeOnSceneOperationBegan(this.Sandbox, this.NetworkSceneOperation);
    this.Sandbox.MainScene.DestroyAllObjects();
    if (Network.Instance._startMode == StartMode.MultiplePeers)
    {
      foreach (Scene prevScene in this.PrevScenes)
        this.DestroyObjectsInScene(prevScene);
    }
    this.UnloadOps = new List<ISceneOperation>(this.PrevScenes.Count);
  }

  internal void DestroyObjectsInScene(Scene scene)
  {
    foreach (GameObject gameObject in ((IEnumerable<GameObject>) scene.GetRootGameObjects()).ToArray<GameObject>())
    {
      if ((Object) gameObject.gameObject != (Object) null)
        Object.Destroy((Object) gameObject);
    }
  }

  void SceneOperation.Update()
  {
    if (!this.LoadOp.IsDone)
      return;
    if (!this.LoadedScene)
    {
      this.NewScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
      if (Network.Instance._startMode == StartMode.MultiplePeers)
      {
        foreach (Scene prevScene in this.PrevScenes)
        {
          if (prevScene.IsValid())
          {
            if (prevScene.buildIndex != -1)
              this.UnloadOps.Add(this.Sandbox.NetworkSceneHandler.InvokeUnloadBuildSceneAsync(prevScene));
            else
              this.UnloadOps.Add(this.Sandbox.NetworkSceneHandler.InvokeUnloadCustomSceneAsync(prevScene));
          }
        }
      }
    }
    this.LoadedScene = true;
    bool flag = true;
    if (Network.Instance._startMode == StartMode.MultiplePeers)
    {
      foreach (ISceneOperation unloadOp in this.UnloadOps)
      {
        if (!unloadOp.IsDone)
          flag = false;
      }
    }
    if (!flag)
      return;
    this.PrevScenes.Clear();
    if (!this.UnloadedPrevScene)
      StaticUnitySceneManager.PendingFinishedOp = (SceneOperation) this;
    this.UnloadedPrevScene = true;
    this.IsFinished = true;
  }
}
